﻿using System;

namespace KPI
{
	public class BasePage : System.Web.UI.Page
	{
		private DataAccess.Models.LoginUserInfo _TheCurrentLoginUser;

		public DataAccess.Models.LoginUserInfo TheCurrentLoginUser
		{
			get
			{
				if(_TheCurrentLoginUser == null)
				{
					_TheCurrentLoginUser = KPISiteContext.Current.CurrentUser;
				}

				return _TheCurrentLoginUser;
			}
		}

		protected override void OnPreInit(EventArgs e)
		{
			base.OnPreInit(e);

			Page.Theme = KPISiteContext.Current.AppTheme;
		}

		/// <summary>
		/// 在客户端显示消息
		/// </summary>
		/// <param name="page"></param>
		/// <param name="msg"></param>
		public void ShowMessage(string msg, bool isError)
		{
			if(isError)
			{
				this.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "showError('" + msg + "');", true);
			}
			else
			{
				this.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "showMessage('" + msg + "');", true);
			}
		}

		/// <summary>
		/// 党政网插入一条消息
		/// </summary>
		/// <param name="msg"></param>
		/// <param name="theKPI"></param>
		public void AddLatestInfo(string msg, DataAccess.Models.KPI theKPI)
		{
			if(!string.IsNullOrWhiteSpace(msg) && theKPI != null)
			{
				using(var wjdzzwLatestInfoRepository = new DataAccess.Repository.WJDZZWLatestInfoRepository())
				{
					var url = GetKPIIndexUrl(theKPI.GroupID, theKPI.CreateUserName);

					try
					{
						wjdzzwLatestInfoRepository.AddLatestInfo(Guid.NewGuid().ToString(), msg, url, theKPI.CreateUserName, "KPI管理平台");
					}
					catch
					{
					}
				}
			}
		}

		public string GetKPIIndexUrl(int groupID, string createUserName)
		{
			var result = string.Empty;

			if(groupID > 0 && !string.IsNullOrWhiteSpace(createUserName))
			{
				result = KPISiteContext.Current.RequestURL + "KPIList.aspx?groupid=" + groupID.ToString() + "&username=" + createUserName;
			}

			return result;
		}

		/// <summary>
		/// 禁用右键菜单和复制
		/// </summary>
		public void DisableCopyAndContextMenu()
		{
			this.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "disableCopy();disableContextMenu();", true);
		}
	}
}